package com.wydstation.power.collection;

import com.alibaba.fastjson.JSON;
import com.ghgande.j2mod.modbus.ModbusException;
import com.ghgande.j2mod.modbus.facade.AbstractModbusMaster;
import com.ghgande.j2mod.modbus.procimg.InputRegister;
import com.ghgande.j2mod.modbus.procimg.Register;
import com.ghgande.j2mod.modbus.util.BitVector;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;


public final class StationInfoContext {

    private final AbstractModbusMaster master;
    private final KafkaProducer producer;

    public StationInfoContext(AbstractModbusMaster master, KafkaProducer producer) throws Exception {
        this.master = master;
        this.producer = producer;
    }

    private List<UnitItem> unitList = new ArrayList();

    public void setUnitList(List<UnitItem> unitList) {
        this.unitList = unitList;
    }

    private byte[] read(int type, int unitId, int start, int end) throws ModbusException {
        byte[] bytes = null;
        if (type == 1) {
            BitVector bitVector = this.master.readCoils(unitId, start, end);
            bytes = bitVector.getBytes();
        } else if (type == 2) {
            BitVector bitVector = this.master.readInputDiscretes(unitId, start, end);
            bytes = bitVector.getBytes();
        } else if (type == 3) {
            InputRegister[] inputRegisters = this.master.readInputRegisters(unitId, start, end);
            bytes = install(inputRegisters);
        } else if (type == 4) {
            Register[] registers = this.master.readMultipleRegisters(unitId, start, end);
            bytes = install(registers);
        }
        return bytes;
    }

    private byte[] install(Register[] registers) {
        byte[] bytes = new byte[registers.length*2];
        int postion = 0;
        for (int i = 0; i < registers.length; i++) {
            Register register = registers[i];
            byte[] bytes1 = register.toBytes();
            for (int i1 = 0; i1 < bytes1.length; i1++) {
                bytes[postion] = bytes1[i1];
                postion = postion += 1;
            }
        }
        return bytes;
    }

    private byte[] install(InputRegister[] registers) {
//        byte[] bytes = new byte[registers.length * 2];
        byte[] bytes = new byte[registers.length * 2];
        int postion = 0;
        for (int i = 0; i < registers.length; i++) {
            InputRegister register = registers[i];
            byte[] bytes1 = register.toBytes();
            for (int i1 = 0; i1 < bytes1.length; i1++) {
                bytes[postion] = bytes1[i1];
                postion = postion += 1;
            }
        }
        return bytes;
    }


    public void run( ) {
        unitList.forEach(unitItem -> {
            ByteBuffer allocate = ByteBuffer.allocate(unitItem.getCount() * 2 + 8);
            int unidId = unitItem.getUnidId();
            try {
                int start = unitItem.getStart();
                int end = unitItem.getEnd();
                while (end <= unitItem.getCount()) {
                    byte[] read = this.read(unitItem.getType(), unidId, start, unitItem.getLimit());
                    allocate.put(read);
                    start = end;
                    end = unitItem.getEnd();
                    if (end >= unitItem.getCount()) {
                        break;
                    }
                }
                producer.send(new ProducerRecord("modbus-2", unidId+"", allocate.array()));
                allocate = null;
                unitItem.clean();
            } catch (ModbusException e) {
                e.printStackTrace();
            }
        });


    }


}
